//
//  SkeletonXIBAppDelegate.m
//  SkeletonXIB
//
//  Created by Jim on 7/25/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "Delegate.h"
#import "App.h"
#import "Simulation.h"
#import "GL20Renderer.h"
#import <UIKit/UIKit.h>

#include "GLRotationAnimation.h"

extern Simulation *simulation;
extern GL20Renderer *renderer;
@implementation AppDelegate

@synthesize window;
@synthesize glView;

//CONSTANTS:
#define kFPS			30.0
#define kSpeed			10.0

static CShell *shell = NULL;

- (void) update
{
   
        MATRIX mShip;
    if(animation)
    {
        animation->GetTransform(mShip, [[NSDate date]timeIntervalSince1970]);
    }
    else
    {
        MatrixIdentity(mShip);
    }
    renderer.shipMotionMatrix = mShip;
	if(!shell->UpdateScene())
		printf("UpdateScene error\n");
	
    if(!shell->RenderScene())
		printf("RenderScene error\n");
	
	[glView swapBuffers];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

	
	if(!shell->InitApplication())
		printf("InitApplication error\n");
	
    // Override point for customization after application launch
    [window makeKeyAndVisible];

	// create our rendering timer
	[NSTimer scheduledTimerWithTimeInterval:(1.0 / kFPS) target:self selector:@selector(update) userInfo:nil repeats:YES];	
}



- (void)dealloc {
	if(!shell->QuitApplication())
		printf("QuitApplication error\n");
	
    [window release];
    if(animation)
    {
        delete animation;
    }
    [super dealloc];
}


- (IBAction)swipeDetected:(id)sender {
    UISwipeGestureRecognizer *recog = (UISwipeGestureRecognizer*)sender;
    printf("recog direction:%d\n",recog.direction);
    if(recog.direction == UISwipeGestureRecognizerDirectionRight)
    {
        simulation->MoveRight(0.5f);
    }
    else if(recog.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        simulation->MoveLeft(0.5f);
    }
}

- (IBAction)tapDetected:(id)sender {
    simulation -> Shot();
}

- (IBAction)panDetected:(id)sender {
    UIPanGestureRecognizer *recog = (UIPanGestureRecognizer*)sender;
    CGPoint p = [recog translationInView:glView];
    CGFloat deltaX = 0;
    if(recog.state == UIGestureRecognizerStateBegan || recog.state == UIGestureRecognizerStateEnded)
    {
        panMoved = 0;
    }
    else
    {
        deltaX = p.x - panMoved;
        panMoved = p.x;
    }
    //Following condition is necessary for bound check.
    if(deltaX>0)
    {
        if(lastShipMotion != eShipMoveRight)
        {
            if(animation)
            {
                delete animation;
            }
            animation = new GLRotationAnimation(0.40,Vec3(0,0,1),0.0f,-PI/4);
            animation -> Start([[NSDate date] timeIntervalSince1970]);
            lastShipMotion = eShipMoveRight;
        }
        simulation->MoveRight(deltaX/SHIP_VELOCITY * PlAYFEILD_X_MAX / 160);
    }
    else if(deltaX<0)
    {
        if(lastShipMotion != eShipMoveLeft)
        {
            if(animation)
            {
                delete animation;
            }
            animation = new GLRotationAnimation(0.40,Vec3(0,0,1),0.0f,PI/4);
            animation -> Start([[NSDate date] timeIntervalSince1970]);
            lastShipMotion = eShipMoveLeft;
        }
        
        simulation->MoveLeft((-deltaX)/SHIP_VELOCITY * PlAYFEILD_X_MAX / 160);
    }
    
    if (recog.state == UIGestureRecognizerStateEnded)
    {
        if(lastShipMotion != eShipMoveStay)
        {
            if(animation)
            {
                delete animation;
                animation = NULL;
            }
            if(lastShipMotion == eShipMoveLeft)
            {
                animation = new GLRotationAnimation(0.40,Vec3(0,0,1),PI/4,0.0f);
                animation -> Start([[NSDate date] timeIntervalSince1970]);
            }
            else
            {
                animation = new GLRotationAnimation(0.40,Vec3(0,0,1),-PI/4,0.0f);
                animation -> Start([[NSDate date] timeIntervalSince1970]);
            }
            lastShipMotion = eShipMoveStay;
        }
    }
}
@end
